﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Oplug.CoolTower.Controls
{
    public partial class RossGridView : UserControl
    {
        /// <summary>
        /// 获取或设置 RossGridView 数据源
        /// </summary>
        public object DataSource { get; set; }
        /// <summary>
        /// 获取或设置 RossGridView 宿主对象
        /// </summary>
        public Form Owner { get; set; }
        public string DataGridName { get; set; }
        /// <summary>
        /// 获取或设置 RossGridView 状态标签
        /// </summary>
        public ToolStripLabel StatusLabel { get; set; }
        /// <summary>
        /// 获取 RossGridView 的DataGridView对象
        /// </summary>
        public DataGridView GridView { get; set; }
        public ContextMenuStrip contextMenuStrip { get; set; }
        private Utilities Utility;
        private List<string> SortList;
        public RossGridView()
        {
            InitializeComponent();
        }

        public void OnInit()
        {
            Utility = new Utilities();            
            GridView = dgvM;
            contextMenuStrip = contextMenuStripM;
            SetToolMenu();           
        }

        private void SetToolMenu()
        {
            contextMenuStripM.Enabled = true;
            ToolMenu_Export.Enabled
                = ToolMenu_Select.Enabled
                = ToolMenu_Clear.Enabled
                = ToolMenu_SortAsc.Enabled
                = ToolMenu_SortDesc.Enabled
                = ToolMenu_Statistics.Enabled
                = ToolMenu_Customization.Enabled
                = ToolMenu_Refresh.Enabled
                = dgvM.Rows.Count > 0;
            ToolMenu_SortAsc.Enabled = ToolMenu_SortDesc.Enabled 
                = DataSource != null && DataSource.GetType() == typeof(DataTable);
            ToolMenu_Customization.Enabled = Owner != null && DataSource != null;
            ToolMenu_Statistics.Enabled = StatusLabel != null && DataSource != null;
        }
        /// <summary>
        ///  绑定 RossGridView 数据源
        /// </summary>
        public void BindData()
        {
            SortList = new List<string>();
            dgvM.DataSource = null;
            dgvM.DataSource = DataSource;
            SetToolMenu();            
            if (Owner != null && DataSource != null)
                Utility.SetHeader(dgvM, Owner.Name + DataGridName);
        }
        private void ToolMenu_Export_Click(object sender, EventArgs e)
        {
            Utility.ExportByDGV(dgvM);
        }
        
        private void ToolMenu_Select_Click(object sender, EventArgs e)
        {            
            Utility.DgvSelect(dgvM);
        }

        private void ToolMenu_Refresh_Click(object sender, EventArgs e)
        {
            BindData();
        }

        private void ToolMenu_SortAsc_Click(object sender, EventArgs e)
        {
            Utility.DgvSort(SortList, dgvM, "asc");
        }

        private void ToolMenu_SortDesc_Click(object sender, EventArgs e)
        {
            Utility.DgvSort(SortList, dgvM, "desc");
        }

        private void ToolMenu_Statistics_Click(object sender, EventArgs e)
        {
            if (StatusLabel != null)
                StatusLabel.Text = Utility.DgvStatistics(dgvM);
        }

        private void ToolMenu_Clear_Click(object sender, EventArgs e)
        {
            dgvM.DataSource = null;
            SortList = new List<string>();
            if (StatusLabel != null)
                StatusLabel.Text = "";
            contextMenuStripM.Enabled = false;
        }

        private void ToolMenu_Customization_Click(object sender, EventArgs e)
        {
            FormCustomization form = new FormCustomization(dgvM, Owner.Name + DataGridName);
            form.ShowDialog();
        }

        private void dgvM_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (Owner.GetType().GetInterfaces().Contains(typeof(IRossGridView)))
                ((IRossGridView)Owner).RossGridView_CellDoubleClick();
        }
        private void DgvM_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (Owner.GetType().GetInterfaces().Contains(typeof(IRossGridView)))
                    ((IRossGridView)Owner).RossGridView_CellClick();
            }
            catch { }
           
        }
        private void dgvM_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            SetToolMenu();
        }

        private void dgvM_DataSourceChanged(object sender, EventArgs e)
        {
            SetToolMenu();
        }

        private void ToolMenu_Search_Click(object sender, EventArgs e)
        {
            panelSearh.Visible = true;
            textBoxSearchKey.Focus();
        }

        private void BtnQSearch_Click(object sender, EventArgs e)
        {
            string keys = textBoxSearchKey.Text.Trim();
            int Idx = 0;
            foreach(DataGridViewRow row in dgvM.Rows)
            {
                for(int i = 0; i < row.Cells.Count; i++)
                {
                    if (row.Cells[i].Value != null && row.Cells[i].Value.ToString().Contains(keys))
                    {
                        row.Cells[i].Style.BackColor = Color.Red;
                        row.Cells[i].Style.ForeColor = Color.White;                        
                        Idx = row.Index;
                    }
                    else
                    {
                        row.Cells[i].Style.BackColor = Color.White;
                        row.Cells[i].Style.ForeColor = Color.Black;
                    }
                }
            }
            dgvM.ClearSelection();
            dgvM.Rows[Idx].Selected = true;
            dgvM.FirstDisplayedScrollingRowIndex = Idx;
        }

        private void BtnQClose_Click(object sender, EventArgs e)
        {
            panelSearh.Visible = false;
        }

        private void DgvM_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                var obj = dgvM.CurrentRow.Cells["IsCheck"];
                if (obj != null)
                {
                    bool isChk = Convert.ToBoolean(obj.Value);
                    dgvM.CurrentRow.DefaultCellStyle.BackColor = isChk ? Color.Moccasin : Color.White;
                }
            }
            catch
            {
                if (dgvM.CurrentRow != null)
                    dgvM.CurrentRow.DefaultCellStyle.BackColor = Color.White;
            }
        }
    }
}
